package com.prothink.spring.service.impl;

import com.prothink.classroomdao.ClassRoomDao;
import com.prothink.spring.entity.ClassRoomEntity;
import com.prothink.spring.service.ClassRoomService;
import com.prothink.springconfig.SpringConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @author mayanping
 * @title: ClassRoomServceImpl
 * @projectName spring-last
 * @description: 业务处理类
 * @date 2024/1/1914:28
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class ClassRoomServceImpl implements ClassRoomService {
    private Logger logger = LoggerFactory.getLogger(SpringConfig.class);

    @Autowired
    private ClassRoomDao classRoomDao;


    @Override
    public void add(ClassRoomEntity classRoomEntity) {
        Long startTime = System.currentTimeMillis();
        logger.info("ClassRoomService add classRoomEntity:{}", classRoomEntity);
        System.out.println("111111111111");

        if(classRoomEntity.getClassName().equals("")){
            throw new RuntimeException("班级名称不能为空!");
        }


        classRoomEntity.setCreateTime(new Date());
        classRoomEntity.setUpdateTime(new Date());



        classRoomDao.add(classRoomEntity);

        Long endTime = System.currentTimeMillis();
        Long time = endTime - startTime;

    }

    @Override
    public void deleteById(int id) {
        logger.info("ClassRoomService deleteById id:{}", id);
        if(id == 0){
            throw new RuntimeException("班级id不能为空!");
        }
        classRoomDao.deleteById(id);
    }

    @Override
    public void update(ClassRoomEntity classRoomEntity) {
        logger.info("ClassRoomService update classRoomEntity:{}", classRoomEntity);
        if(classRoomEntity.getId() == 0 ){
            throw new RuntimeException("班级id不能为空!");
        }

        classRoomEntity.setUpdateTime(new Date());
        classRoomDao.update(classRoomEntity);
    }

    @Override
    @Transactional(readOnly = true)
    public List<ClassRoomEntity> selectAll() {
        logger.info("ClassRoomService selectAll");
        List<ClassRoomEntity> result = classRoomDao.selectAll();

        logger.info("ClassRoomService selectAll result:{}", result);
        return result;
    }

    @Override
    @Transactional(readOnly = true)
    public ClassRoomEntity selectById(int id) {
        logger.info("ClassRoomService selectById id:{}", id);

        if(id == 0 ){
            logger.warn("ClassRoomService selectById id为空! ");
            throw new RuntimeException("班级id不能为空!");
        }

        ClassRoomEntity classRoomEntity =  classRoomDao.selectById(id);

        logger.info("ClassRoomService selectById result:{}", id);
        return classRoomEntity;
    }

    @Override
    public void updateStudentCountById(int classId, int studentCount) {
        if(classId == 0){
            throw new RuntimeException("班级id不能为空!");
        }
        if(studentCount < 0){
            throw new RuntimeException("班级学生数量studentCount不能为负数!");
        }

        logger.info("ClassRoomService updateStudentCountById classId:{}, studentCount:{}", classId, studentCount);
        classRoomDao.updateStudentCountById(classId, studentCount);
    }
}

